/* Copyright (C) 2008 Sahid Ferdjaoui <sahid@funraill.org>
 *
 *   This file is part of Postmemd.
 *
 * Postmemd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef PM_CORE_H
#define PM_CORE_H


/**
 * @file pm_core.h
 * @brief Core object
 */


#include "pm_head.h"
#include "pm_poll.h"
#include "pm_slaves.h"

#include <ev.h>
#include <json.h>




/**
 * Object Core
 */
typedef struct core {
	sfd_t			           sfd;                  /** client listening */
	state_t		           state;                /** core state */
	slave_t            * slave;                /** slave object */
	ev_io			           event;                /** event structure */
	struct ev_loop	   * loop;                 /** default event loop */
	struct json_object * json_obj;
	int				           cmd;                  /** execute command */
	char                 key[41];              /** key in hash table */
	char               * rbuffer;              /** read buffer */
	int                  rbuffer_bytes;        /** number of bytes readed */
	int                  writed;

	char               * wbuffer;              /** write buffer */
	int                  wbuffer_bytes;        /** number of bytes writed */
	int				           closed;               /** close core flag */

} core_t;



/**
 * Process loop...
 * @param core The core object.
 */
void pm_core_process_loop (core_t * core);

/**
 *
 *
 *
 */
int pm_core_init (struct ev_loop * loop, sfd_t sfd, state_t state, core_t ** core);

/**
 *
 *
 *
 */
void pm_core_close (core_t * core);

/**
 *
 *
 */
int pm_core_parse_json (char * buffer, core_t ** core);

/**
 *
 *
 */
int pm_core_json_cmd (struct json_object * json_obj, core_t ** core);

#endif
